package com.example.controller;

import com.example.entity.result.Result;
import com.example.entity.result.ResultBean;
import com.example.entity.vo.User;
import com.example.exception.OperatorException;
import com.example.service.*;
import com.example.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据访问控制
 */
@RestController
@RequestMapping("/data")
public class DataController extends BaseHandleException {
    @Autowired
    private UserService userService;
    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private CarTypeService carTypeService;
    @Autowired
    private CarService carService;
    @Autowired
    private OrderService orderService;

    //登录检测
    private void check(HttpServletRequest request) {
        if (request.getSession().getAttribute("user") == null) {
            throw new OperatorException("please sign in", Result.USER_NOT_SING_IN);
        }
    }

    /**
     * 综合查询
     *
     * @param obj   查询对象
     * @param key   查询方式
     * @param value 查询参数
     */
    @RequestMapping("/query")
    public ResultBean queryData(@RequestParam("obj") String obj,
                                @RequestParam("key") String key,
                                @RequestParam(value = "value", required = false, defaultValue = "NAN") String value,
                                HttpServletRequest request) {
        if (StringUtils.isEmpty(obj)) {
            throw new OperatorException("参数为空", Result.PARAMETER_EMPTY);
        }
        switch (obj) {
            case "user":
                return DataUtils.getResultBean(userService.query(key, value), obj);
            case "userType":
                return DataUtils.getResultBean(userTypeService.query(key, value), obj);
            case "carType":
                return DataUtils.getResultBean(carTypeService.query(key, value), obj);
            case "car":
                return DataUtils.getResultBean(carService.query(key, value), obj);
            case "order":
                return DataUtils.getResultBean(orderService.query(key, value), obj);
            default:
                return DataUtils.getResultBean(Result.PARAMETER_INVALID, "");
        }
    }

    //用户查询
    @RequestMapping("/user")
    public ResultBean queryUser(@RequestParam("key") String key,
                                @RequestParam(value = "value", required = false, defaultValue = "NAN") String value,
                                HttpServletRequest request) {
        check(request);
        return DataUtils.getResultBean(userService.query(key, value), "user");
    }

    //用户类型查询
    @RequestMapping("/userType")
    public ResultBean queryUserType(@RequestParam("key") String key,
                                    @RequestParam(value = "value", required = false,
                                            defaultValue = "NAN") String value) {
        return DataUtils.getResultBean(userTypeService.query(key, value), "userType");
    }

    //汽车类型查询
    @RequestMapping("/carType")
    public ResultBean queryCarType(@RequestParam("key") String key,
                                   @RequestParam(value = "value", required = false,
                                           defaultValue = "NAN") String value) {
        return DataUtils.getResultBean(carTypeService.query(key, value), "carType");
    }

    @RequestMapping("/text")
    public ResultBean queryText(@RequestParam("json") String json, HttpServletRequest request) {
        if (request.getSession().getAttribute("key") == null){
            request.getSession().setAttribute("key", "json");
            System.out.println("同一用户");
        }

        return DataUtils.getResultBean(Result.SUCCESS, json);
    }

    @RequestMapping("/text3")
    public ResultBean queryTex3() {
        User user = (User) DataUtils.textUtils(User.class);
        return DataUtils.getResultBean(Result.SUCCESS, "user", "", user);
    }

    @RequestMapping("/text2")
    public ResultBean queryText2() {
        List<User> list = new ArrayList<>();
        list.add(DataUtils.textUtils(User.class));
        list.add(DataUtils.textUtils(User.class));
        list.add(DataUtils.textUtils(User.class));
        return DataUtils.getResultBean(Result.SUCCESS, "user", "", list);
    }

    //汽车查询
    @RequestMapping("/car")
    public ResultBean queryCar(@RequestParam("key") String key,
                               @RequestParam(value = "value", required = false, defaultValue = "NAN") String value,
                               HttpServletRequest request) {
        check(request);
        return DataUtils.getResultBean(carService.query(key, value), "car");
    }

    //订单查询
    @RequestMapping("/order")
    public ResultBean queryOrder(@RequestParam("key") String key,
                                 @RequestParam(value = "value", required = false, defaultValue = "NAN") String value,
                                 HttpServletRequest request) {
        check(request);
        return DataUtils.getResultBean(orderService.query(key, value), "order");
    }
}

